﻿package src {
	
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import src.uipackage.ui.events.ColorEvent;
	import src.uipackage.ui.events.ValueSliderEvent;
	import src.uipackage.ui.OBO_ColorPicker;
	import src.uipackage.ui.OBO_ValueSlider;
	
	/**
	 * Class for the legend tab content
	 * @author Indrek Tamming
	 */
	public class LegendTab extends Tab {
		
		/**
		 * Callback to Viewer
		 */
		private var cb:Viewer;
		
		/**
		 * An array on MotifMatches objects
		 */
		private var motifMatchesArray:Array;
		
		/**
		 * Array of filter labels for quick change
		 */
		private var filterLabels:Array;
		
		/**
		 * Array of name fields
		 */
		private var nameFields:Array;
		
		/**
		 * Array of logos
		 */
		private var logos:Array;
		
		/**
		 * Init the legend, add listener and draw
		 * @param	cb					-	Viewer callback
		 * @param	motifMatchesArray	-	array of MotifMatches
		 */
		public function LegendTab(cb:Viewer, motifMatchesArray:Array):void {
			this.cb = cb;
			this.motifMatchesArray = motifMatchesArray;
			var _length:int = motifMatchesArray.length;
			logos = new Array(_length);
			filterLabels = new Array(_length);
			nameFields = new Array(_length);
			var filterLabelFormat:TextFormat = new TextFormat();
			filterLabelFormat.size = 9.5;
			filterLabelFormat.font = "Arial";
			var nameFieldFormat:TextFormat = new TextFormat();
			nameFieldFormat.font = "Arial";
			nameFieldFormat.size = 12;
			nameFieldFormat.bold = true;
			for (var i:int = 0; i < _length; i++) {
				logos[i] = motifMatchesArray[i].getMotif().drawLogo();
				logos[i].scaleX = logos[i].scaleY = 0.18;
				var filterLabel:TextField = new TextField();
				filterLabel.selectable = false;
				filterLabel.autoSize = TextFieldAutoSize.LEFT;
				filterLabel.defaultTextFormat = filterLabelFormat;
				filterLabels[i] = filterLabel;
				var nameField:TextField = new TextField();
				nameField.selectable = false;
				nameField.autoSize = TextFieldAutoSize.LEFT;
				nameField.defaultTextFormat = nameFieldFormat;
				nameFields[i] = nameField;
			}
		}
		
		/**
		 * Draw legen tab UI
		 */
		public function draw():void {
			while (this.numChildren > 0) this.removeChildAt(0);
			
			addChild(drawButton(-8, 10, hideAllMotifs));
			addChild(drawLabel("Hide all", 7, 7));
			
			addChild(drawButton(54, 10, inverseMotifs));
			addChild(drawLabel("Inverse", 69, 7));
			
			const yIncreaser:Number = 84;
			for (var i:int = 0 ; i < motifMatchesArray.length ; i++) { //TODO liigutaks lihtsalt motifContainer y koordinaati?
				var motif:Motif = MotifMatches(motifMatchesArray[i]).getMotif();
				var motifContainer:MovieClip = new MovieClip(); // so we can add dynamic variables like Motif etc
				motifContainer.y = 28;
				motifContainer.name = motif.getName();
				motifContainer.motifMatchesCallback = MotifMatches(motifMatchesArray[i]);
				motifContainer.loopIndex = i;
				
				nameFields[i].y = 5 + i * yIncreaser;
				var tf:TextFormat = nameFields[i].getTextFormat();
				tf.color = motifMatchesArray[i].getColor();
				nameFields[i].defaultTextFormat = tf;
				nameFields[i].text = motif.getName();
				motifContainer.addChild(nameFields[i]);
				
				var logo:Sprite = logos[i];
				logo.y = 58 + i * yIncreaser;
				motifContainer.addChild(logo);
				
				var showHideBtn:MovieClip = new ShowHideBtn();
				showHideBtn.y = 23 + i * yIncreaser;
				showHideBtn.x = 100;
				showHideBtn.buttonMode = true;
				if (motifContainer.motifMatchesCallback.getHide()) showHideBtn.gotoAndStop("Hide");
				else showHideBtn.gotoAndStop("Show");
				showHideBtn.addEventListener(MouseEvent.CLICK, showHideBtnClicked);
				motifContainer.addChild(showHideBtn);
				
				var colorPicker:OBO_ColorPicker = new OBO_ColorPicker(motifMatchesArray[i].getColor(), cb.innerAlpha);
				colorPicker.setMotifMatchesCb(motifMatchesArray[i]);
				colorPicker.addEventListener(ColorEvent.COLOR_SELECT, colorSelect);
				colorPicker.y = 33 + i * yIncreaser;
				colorPicker.x = 100;
				motifContainer.addChild(colorPicker);
				
				
				filterLabels[i].y = 58 + i * yIncreaser;
				if (motifContainer.motifMatchesCallback.getFilter() == motifContainer.motifMatchesCallback.getMaxScore() + 5) {
					filterLabels[i].text = "SWITCHED OFF";
				}
				else {
					filterLabels[i].text = "FILTER: score > " + Math.round(motifContainer.motifMatchesCallback.getFilter() * 100) / 100;
				}
				motifContainer.addChild(filterLabels[i]);
				
				var matches:MotifMatches = MotifMatches(motifMatchesArray[i]);
				var filterSlider:OBO_ValueSlider = 
					new OBO_ValueSlider(115, matches.getMaxScore() + 0.5, matches.getMinScore() - 0.5,  matches.getFilter());
				filterSlider.addEventListener(ValueSliderEvent.DRAG, legendFilterDrag);
				filterSlider.addEventListener(ValueSliderEvent.STOPDRAG, legendFilterStopDrag);
				filterSlider.y = 70 + i * yIncreaser;
				motifContainer.addChild(filterSlider);
				
				addChild(motifContainer);
			}
		}
		
		/**
		 * Color select event handler
		 * @param	e
		 */
		private function colorSelect(e:ColorEvent):void {
			e.matches.setColor(e.color);
			cb.uiIsDirty = true;
			cb.viewIsDirty = true;
		}
		
		/**
		 * Show/hide motif button event handler
		 * @param	e	-	click
		 */
		private function showHideBtnClicked(e:MouseEvent):void {
			var btn:MovieClip = e.target as MovieClip;
			var motifMatches:MotifMatches = MovieClip(MovieClip(e.target).parent).motifMatchesCallback;
			if (btn.currentLabel == "Show") {
				btn.gotoAndStop("Hide");
				motifMatches.setHide(true);
			}
			else {
				btn.gotoAndStop("Show");
				motifMatches.setHide(false);
			}
			cb.uiIsDirty = true;
			cb.viewIsDirty = true;
		}
		
		/**
		 * Filter slider dragging event handler
		 * @param	e
		 */
		private function legendFilterDrag(e:ValueSliderEvent):void {
			var motifMatches:MotifMatches = e.target.parent.motifMatchesCallback;
			motifMatches.setFilter(e.value);
			filterLabels[int(e.target.parent.loopIndex)].text =
				"FILTER: score > " + Math.round(e.value * 100) / 100;
		}
		
		/**
		 * Filter slider stopped dragging
		 * @param	e
		 */
		private function legendFilterStopDrag(e:ValueSliderEvent):void {
			var motifMatches:MotifMatches = e.target.parent.motifMatchesCallback;
			motifMatches.setFilter(e.value);
			cb.viewIsDirty = true;
			cb.uiIsDirty = true;
		}
		
		/**
		 * Hide all motifs button clicked
		 * @param	e
		 */
		private function hideAllMotifs(e:MouseEvent = null):void {
			for (var i:int = 0; i < cb.motifMatchesArray.length; i++) {
				var motifMatches:MotifMatches = cb.motifMatchesArray[i];
				motifMatches.setHide(true);
			}
			cb.viewIsDirty = true;
			cb.uiIsDirty = true;
		}
		
		/**
		 * Inverse motifs button clicked
		 * @param	e
		 */
		private function inverseMotifs(e:MouseEvent = null):void {
			for (var i:int = 0; i < cb.motifMatchesArray.length; i++) {
				var motifMatches:MotifMatches = cb.motifMatchesArray[i];
				motifMatches.setHide(motifMatches.getHide()?false:true);
			}
			cb.viewIsDirty = true;
			cb.uiIsDirty = true;
		}
		
	}
	
}